package ch08
{
	import flash.display.Sprite;
	import flash.events.Event;

	import utils.Ball;
	/**
	 * 一维坐标上的弹性运动 
	 * @author sjp
	 * 
	 */
	public class Spring1 extends Sprite
	{
		private var ball:Ball;
		//弹性
		private var spring:Number=0.1;
		private var targetX:Number=stage.stageWidth / 2;
		private var vx:Number=0;
		//摩擦力
		private var friction:Number=0.95;

		public function Spring1()
		{
			init();
		}

		private function init():void
		{
			ball=new Ball();
			addChild(ball);
			ball.y=stage.stageHeight / 2;
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onEnterFrame(event:Event):void
		{
			var dx:Number=targetX - ball.x;
			//加速度与距离成正比，也就是距离乘以 spring 的值
			var ax:Number=dx * spring;
			vx+=ax;
			vx*=friction;
			ball.x+=vx;
		}
	}
}